/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package server;

import client.ClientOd;
import commun.Message;
import frame.Fserver;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.RemoteException;
import java.util.ArrayList;
import user.*;


/**
 *
 * @author  Qiao BingZhi
 */
public abstract class $ZoneExchange extends UnicastRemoteObject implements _ZoneExchange {

    private UserManager userManager=null;
    private ServerManager serverManager=null;


    private Fserver f =null;
    /**
     * ZoneExchange's constructor
     * @throws RemoteException
     */
    public $ZoneExchange() throws RemoteException{
        super();
        userManager=new UserManager();
        serverManager=new ServerManager();
    }

    /**
     * Register a new user with his password
     * @param user an user with their attributes
     * @param pwd user's password
     * @return user's name
     * @throws RemoteException
     */
    public String registerUser(User user,String pwd) throws RemoteException {
            String tmp=null;
          try
          {
               tmp=userManager.newUser(user,pwd);
          }
          catch(Exception e)
          {
            e.printStackTrace();
          }
            if(tmp!=null)
            {
              this.f.updateUsers(this.getAllUsers());

            }
            return tmp;
    }

    /**
     * Return a table with all users connected.
     * @return a table of users
     * @throws RemoteException
     */
    public User[] getAllUsers() throws RemoteException {
           User[] tmp = null;
          try
          {
            tmp=userManager.getAllUsers();
          }
          catch(Exception e)
          {
            e.printStackTrace();
          }
           return tmp;
    }
    /**
     * Return a table with all friends connected.
     * @param id id of the friends's user
     * @return a table of users
     * @throws RemoteException
     */
    public User[] getAllFriends(String id) throws RemoteException {
          User[] tmp=null;
          try
          {
            tmp=userManager.getAllFriends(id);
          }
          catch(Exception e)
          {
            e.printStackTrace();
          }
         return tmp;
    }
    /**
     * Add a contact as a friend to communicate with him
     * @param id1 id of user who want to add a contact in his friends list
     * @param id2 the contact to add in friends list
     * @return true if the operation is fine, false otherwise
     * @throws RemoteException
     */
    public boolean addFriend(String id1, String id2) throws RemoteException {
         boolean flag=false;
         try
         {
             flag=userManager.addFriend(id1, id2);
         }
         catch(Exception e)
         {
            e.printStackTrace();
         }
         return flag;
    }
    /**
     * Send a message to an user
     * @param message the message to send
     * @return true if the operation is fine, false otherwise
     * @throws RemoteException
     */
    public boolean sendMessage(Message message) throws RemoteException {
          boolean flag=false;
          String id=message.getToId();
          ClientOd client=null;
         try
         {
            if(serverManager.existClient(id))
            {
               client=serverManager.getClient(id);
               client.ReceiveMessage(message);
               flag=true;
            }
            else
            {
                userManager.MessageSaver(message);
                flag=true;
            }
         }
         catch(Exception e)
         {
           e.printStackTrace();
         }
         return flag;
    }


    public boolean logout(String id) throws RemoteException {
        boolean flag= false;

       try
       {
         flag=serverManager.removeClient(id);

       }
       catch(Exception e)
       {
         e.printStackTrace();
       }
       if(flag)
       {
         this.f.removeOnlineUser(id);
       }
       return flag;
    }

    public User getUserById(String id) throws RemoteException {
          User tmp =new User();
          try
          {
            tmp=userManager.getUserById(id);
          }
          catch(Exception e)
          {
            e.printStackTrace();
          }
          return tmp;
    }

    // return -1 fail; return 1 ok; return 0 already login,return 2 login not correct

    public int login(Login login, ClientOd client) throws RemoteException {
         int flag=-1;
         String id=login.getUserId();
         try
         {
           
           if(serverManager.existClient(id))
           {
              flag=0;
           }
           else
           {
             if(userManager.checkLogin(login))
             {

                serverManager.addClient(id, client);

                flag=1;
             }
             else
             {
                flag=2;
             }
           }


         }
         catch(Exception e)
         {
            e.printStackTrace();
         }

         if(this.f!=null && flag==1)
         {
            User tmpU=new User();
            tmpU=this.getUserById(id);
            System.out.println(tmpU.getUserId());
            System.out.println(tmpU.getUserName());
            this.f.addOnlineUser(tmpU);
         }

         return flag;
    }
    public void getOfflineMsg(String id)throws RemoteException
    {
       ArrayList tmp =userManager.MessageGetter(id);
		for (int i = 0; i < tmp.size(); i++) {
			Message m = (Message) tmp.get(i);
			this.sendMessage(m);
		}


    }
    public void setFrame(Fserver f)throws RemoteException{

         this.f=f;
    }

    public String testMethode(String test) throws RemoteException {
        try {
            System.out.println("Le test est commencé ");
            System.out.println(test);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return test;
    }
}
